<template>
	<view class="content">
		<textarea name="value" v-model="value" placeholder="输入信息" class="textare" cols="30" rows="10"></textarea>
		<view class="fun-model">
			<button hover-class="hovers" @click="write(false)" :class="(!status ? 'disabled':'')">发送消息</button>
			<button hover-class="hovers" @click="connect">连接</button>
			<button hover-class="hovers" @click="close">断开</button>
		</view>
		<view style="margin-bottom: 24rpx;padding-left: 24rpx;">操作日志：</view>
		<textarea name="content" v-model="content" placeholder="暂无内容" disabled class="textare" cols="30" rows="100"></textarea>
	</view>
</template>

<script>
	var Socket = plus.android.importClass("java.net.Socket");
	var PrintWriter = plus.android.importClass("java.io.PrintWriter");
	var BufferedWriter = plus.android.importClass("java.io.BufferedWriter");
	var OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");
	var BufferedReader = plus.android.importClass("java.io.BufferedReader");
	var InputStreamReader = plus.android.importClass("java.io.InputStreamReader");
				
	//测试改良  
	var StrictMode = plus.android.importClass("android.os.StrictMode");
	var Build = plus.android.importClass("android.os.Build");
	
	var socket = null, socketWriter = null, socketReader = null;
	const h = "192.168.1.3", p = 9998;
	export default {
		data() {
			return {
				title: 'ping tcp',
				value: "ff",
				content: "",
				status: false
			}
		},
		onLoad() {
			
		},
		methods: {
			addlog(val) {
				this.content += (val+'\n')
			},
			write(val) {
				// var outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
				// var bufferWriter = new BufferedWriter(outputStreamWriter);
				// var socketWriter = new PrintWriter(bufferWriter, true);
				let v = (val ? val : this.value)
				console.log('写',v)
				socketWriter.println(v.replace(/\\|\n|\r|\t|\f|\t/g, ''));
				this.addlog(val ? '发送心跳包:'+val : '发送信息:'+this.value)
				this.read()
			},
			read() {
				// var inputStreamReader = new InputStreamReader(socket.getInputStream());
				// var socketReader = new BufferedReader(inputStreamReader);
				var msgBeReceived = socketReader.readLine();
				console.log(msgBeReceived);
				if(msgBeReceived != null) {
					this.addlog('接收信息:'+msgBeReceived)
					this.status = true
					plus.nativeUI.toast('连接成功')
				} else {
					if(this.status) {
						this.status = false;
					}
				}
			},
			close() {
				socket.close()
			},
			heart() {
				setInterval(()=>{
					this.write('9')
				},1000*3)
			},
			connect() {
				if (Build.VERSION.SDK_INT > 9) {
					var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
					StrictMode.setThreadPolicy(policy);
				}
				console.log("连接",h+':'+p)
				if(socket!= null) {
					socket.close()
				}
				socket = new Socket(h, p);
				socket.setSoTimeout(5000);
				console.log('连接状态')
				
				
				
				
				// 写
				var outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
				var bufferWriter = new BufferedWriter(outputStreamWriter);
				socketWriter = new PrintWriter(bufferWriter, true);
				// socketWriter.println("FF");
				
				// 读
				var inputStreamReader = new InputStreamReader(socket.getInputStream());
				socketReader = new BufferedReader(inputStreamReader);
				// var msgBeReceived = socketReader.readLine();
				// console.log(msgBeReceived);
				
				this.heart()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 24rpx 24rpx;
		box-sizing: border-box;
		.textare {border: 1rpx solid #eee;border-radius: 12rpx;width: 100%;padding: 12rpx 24rpx;box-sizing: border-box;}
		.fun-model {
			margin: 24rpx 0;
			button {background: $uni-color-primary;color: #fff;border: none;margin-bottom: 12rpx;font-size: 32rpx;}
			.disabled {background: #eee;opacity: 0.8;color: #aaa;}
		}
	}
</style>
